package com.hzjy.manage.service.impl;


import com.hzjy.api.beans.HzjyProject;
import com.hzjy.api.mapper.ApiProjectMapper;
import com.hzjy.api.service.impl.ApiDelistServiceImpl;
import com.hzjy.common.enums.DelistingStatusEnum;
import com.hzjy.common.enums.TradingStatusEnum;
import com.hzjy.common.utils.PageData;
import com.hzjy.manage.mapper.HzjyDelistMapper;
import com.hzjy.manage.mapper.HzjyProductMapper;
import com.hzjy.manage.vo.HzjyDelist;
import com.hzjy.manage.vo.HzjyDelistVO;
import com.hzjy.manage.vo.HzjyProduct;
import com.hzjy.manage.vo.HzjyProductDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class HzjyProductServiceImpl {

    @Autowired
    private HzjyProductMapper hzjyProductMapper;

    @Autowired
    private HzjyDelistMapper hzjyDelistMapper;
    @Autowired
    private ApiProjectMapper apiProjectMapper;
    @Autowired
    private ApiDelistServiceImpl apiDelistServiceImpl;

    /**
     * 面议价格
     *
     * @param pd
     */
    public void updateHzjyProduct(PageData pd) {

        HzjyProduct hzjyProduct = new HzjyProduct();
        //修改价格和价格类型
        /*hzjyProduct.setProductId(Long.parseLong(pd.get("productId").toString()));
        hzjyProduct.setPriceType(0);
        BigDecimal price = new BigDecimal(pd.get("price").toString()).multiply(new BigDecimal(100));
        hzjyProduct.setPrice(Integer.parseInt(price.toString()) );
        hzjyProductMapper.updateHzjyProduct(hzjyProduct);*/
        HzjyDelist hzjyDelist = new HzjyDelist();
        hzjyDelist.setDelistId(Long.parseLong(pd.get("delistId").toString()));
        hzjyDelist.setPriceTable(pd.get("price").toString());
        hzjyDelist.setStatus(5);//价格磋商完成
        hzjyDelistMapper.updateHzjyDelist(hzjyDelist);
    }

    /**
     * 查询终审通过列表
     *
     * @param hzjyDelist
     * @return
     */
    public List<HzjyDelistVO> selectHzjyDelistFinalList(HzjyDelistVO hzjyDelist) {
        List<HzjyDelistVO> hzjyDelistVOS = hzjyDelistMapper.selectHzjyDelistFinalList(hzjyDelist);
        for (HzjyDelistVO hzjyDelistVO : hzjyDelistVOS) {
            Integer price = hzjyDelistVO.getPrice();
            BigDecimal divide = new BigDecimal(price).divide(new BigDecimal(100));
            hzjyDelistVO.setPriceStr(divide.toString());
        }
        return hzjyDelistVOS;
    }

    /**
     * 挂牌清单列表
     *
     * @return
     */
    public List<HzjyProductDTO> selectProductList(String productName, Integer tradingType) {
        List<HzjyProductDTO> hzjyProductList = hzjyProductMapper.selectProductList(productName, tradingType);
        return hzjyProductList;
    }

    /**
     * 上传备案判断挂牌方式（转让/许可） 权力是否下架
     *
     * @param productId
     */
    public void selectProductInfo(Long productId) {
        HzjyProduct hzjyProduct = hzjyProductMapper.selectHzjyProductById(productId);
        if (hzjyProduct.getTradingType() == 1) {
            //项目
            HzjyProject queryProject = new HzjyProject();
            queryProject.setProductId(hzjyProduct.getProductId().toString());
            List<HzjyProject> hzjyProjectList = apiProjectMapper.selectHzjyProjectList(queryProject);
            HzjyProject hzjyProject = hzjyProjectList.get(0);
            hzjyProduct.setHzjyProject(hzjyProject);

            //查询作品拥有的权力
            String[] salePower = hzjyProject.getTradingPower().split(",");
            List<String> salePowerList = Arrays.asList(salePower);
            List<String> pList = new ArrayList<String>(salePowerList);
            com.hzjy.api.beans.HzjyDelist queryDelist = new com.hzjy.api.beans.HzjyDelist();
            queryDelist.setProductId(hzjyProduct.getProductId());
            //已经摘牌申请的产品
            List<com.hzjy.api.beans.HzjyDelist> haveDelist = apiDelistServiceImpl.selectHzjyDelistList(queryDelist);

            // 查询作品可出售权利
            if (hzjyProduct.getTermType() == 1) {
                //许可
                if (hzjyProject.getPermissionType() == 0) {
                    //专有许可
                    if (!CollectionUtils.isEmpty(haveDelist)) {
                        //已经有人摘牌当前许可
                        //判断当前摘牌的权利跟
                        for (com.hzjy.api.beans.HzjyDelist delist : haveDelist) {
                            if (delist.getStatus() != DelistingStatusEnum.Audit.getCode() && delist.getStatus() != DelistingStatusEnum.FirstTrial.getCode()){
                                String[] delistPower = delist.getDelistPower().split(",");
                                for (String power : delistPower) {
                                    if (salePowerList.contains(power)) {
                                        pList.remove(power);
                                    }
                                }
                            }
                        }
                    }
                    if (CollectionUtils.isEmpty(pList)) {
                        //权力已经被摘完 将作品状态改成下架
                        hzjyProduct.setUpState(0);
                    }
                }
            } else {
                //转让
                if (!CollectionUtils.isEmpty(haveDelist)) {
                    //已经有人摘牌当前许可
                    //判断当前摘牌的权利跟
                    for (com.hzjy.api.beans.HzjyDelist delist : haveDelist) {
                        if (delist.getStatus() != DelistingStatusEnum.Audit.getCode() && delist.getStatus() != DelistingStatusEnum.FirstTrial.getCode()){
                            String[] delistPower = delist.getDelistPower().split(",");
                            for (String power : delistPower) {
                                if (salePowerList.contains(power)) {
                                    pList.remove(power);
                                }
                            }
                        }
                    }
                }
                if (CollectionUtils.isEmpty(pList)) {
                    //权力已经被摘完 将作品状态改成下架
                    hzjyProduct.setUpState(0);
                }
            }
        } else {
            //作品  直接下架
            hzjyProduct.setUpState(0);
        }
        //修改数据库
        hzjyProductMapper.updateHzjyProduct(hzjyProduct);
    }
}
